<script setup>
import { ref, onMounted } from 'vue'
import articleClassifyApi from '@/apis/admin/articleClassifyApi.js'
import commonConstant from '@/enums/commonConstant.js'
import PaginationComponent from '@/components/PaginationComponent.vue'

const articleClassifySearchCondition = ref({
  articleClassifyName: ''
})
const paginationData = ref({ ...commonConstant.PAGINATION })
const articleClassifyTableList = ref([])
const drawerData = ref({
  visible: false,
  title: ''
})
const waitUpdateArticleClassifyData = ref({})
const waitOperationArticleClassifyList = ref([])

onMounted(async () => {
  await handlePageSelectArticleClassify()
})

// 批量删除文章分类
const handleBatchDeleteArticleClassify = async () => {
  await ElMessageBox.confirm(`是否确认执行该操作?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const articleClassifyIdList = waitOperationArticleClassifyList.value.map(
        (e) => {
          return e.articleClassifyId
        }
      )
      await articleClassifyApi.batchDeleteArticleClassify(articleClassifyIdList)
      await handlePageSelectArticleClassify()
    })
    .catch(() => {})
}

// 删除文章分类
const handleDeleteArticleClassify = async (articleClassifyId) => {
  await ElMessageBox.confirm(`是否确认执行该操作?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await articleClassifyApi.deleteArticleClassify(articleClassifyId)
      await handlePageSelectArticleClassify()
    })
    .catch(() => {})
}

// 提交抽屉（完成新增或修改）
const handleSubmitDrawer = async () => {
  if (waitUpdateArticleClassifyData.value.articleClassifyId) {
    await handleUpdateArticleClassify()
  } else {
    await handleInsertArticleClassify()
  }

  await handlePageSelectArticleClassify()
  await handleCloseDrawer()
}

// 修改文章分类
const handleUpdateArticleClassify = async () => {
  await articleClassifyApi.updateArticleClassify(
    waitUpdateArticleClassifyData.value
  )
}

// 新增文章分类
const handleInsertArticleClassify = async () => {
  await articleClassifyApi.insertArticleClassify(
    waitUpdateArticleClassifyData.value
  )
}

// 关闭抽屉
const handleCloseDrawer = () => {
  waitUpdateArticleClassifyData.value = {}
  drawerData.value.visible = false
}

// 打开抽屉
const handleOpenDrawer = (data) => {
  waitUpdateArticleClassifyData.value = { ...data }
  if (data.articleClassifyId) {
    drawerData.value.title = '更新文章分类'
  } else {
    drawerData.value.title = '新增文章分类'
  }
  drawerData.value.visible = true
}

// 分页查询文章分类
const handlePageSelectArticleClassify = async () => {
  const response = await articleClassifyApi.pageSelectArticleClassify(
    paginationData.value.currentPage,
    paginationData.value.pageSize,
    articleClassifySearchCondition.value
  )

  articleClassifyTableList.value = response.records
  paginationData.value.total = response.total
}

// 表格选择发送改变
const handleSelectionChange = (newSelection) => {
  waitOperationArticleClassifyList.value = newSelection
}

// 切换页面大小
const handleSizeChange = (value) => {
  paginationData.value.pageSize = value
  handlePageSelectArticleClassify()
}

// 切换页码
const handleCurrentChange = (value) => {
  paginationData.value.currentPage = value
  handlePageSelectArticleClassify()
}
</script>
<template>
  <div class="article-classify-manage">
    <div class="search-condition-item">
      <el-form :inline="true" :model="articleClassifySearchCondition">
        <el-form-item label="文章分类名称">
          <el-input
            v-model="articleClassifySearchCondition.articleClassifyName"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handlePageSelectArticleClassify">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="operation-item">
      <div v-if="waitOperationArticleClassifyList.length > 0">
        <el-button type="danger" @click="handleBatchDeleteArticleClassify">
          批量删除
        </el-button>
      </div>
      <div v-else>
        <el-button
          type="primary"
          @click="handleOpenDrawer(waitUpdateArticleClassifyData)"
        >
          新增
        </el-button>
      </div>
    </div>

    <div class="table-item">
      <el-table
        :data="articleClassifyTableList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="articleClassifyName" label="文章分类名称" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="handleOpenDrawer(scope.row)"
            >
              修改
            </el-button>
            <el-button
              link
              type="danger"
              size="small"
              @click="handleDeleteArticleClassify(scope.row.articleClassifyId)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-item">
      <PaginationComponent
        :paginationData="paginationData"
        @sizeChange="handleSizeChange"
        @currentChange="handleCurrentChange"
      ></PaginationComponent>
    </div>

    <div class="drawer-item">
      <el-drawer
        v-model="drawerData.visible"
        :show-close="false"
        @close="handleCloseDrawer"
      >
        <template #header>
          <h4>{{ drawerData.title }}</h4>
        </template>
        <el-form label-width="auto" :model="waitUpdateArticleClassifyData">
          <el-form-item label="文章分类名称">
            <el-input
              v-model="waitUpdateArticleClassifyData.articleClassifyName"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleCloseDrawer">取消</el-button>
            <el-button type="primary" @click="handleSubmitDrawer">
              确认</el-button
            >
          </el-form-item>
        </el-form>
      </el-drawer>
    </div>
  </div>
</template>
<style scoped>
.search-condition-item .el-input {
  width: 210px;
}
.el-table {
  width: 100%;
  height: 451px;
}
</style>
